package com.example.lin.ximalaya.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lin.ximalaya.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    Unbinder unbinder;
    private HomeFragmentPagerAdapter homeFragmentPagerAdapter;

    public HomeFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageItems.add("分类");
        pageItems.add("推荐");
        pageItems.add("VIP");
        pageItems.add("直播");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
        pageItems.add("小说");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        /**
         *         碎片中 管理 子碎片. 获取 子碎片的个管理对象
         */
        homeFragmentPagerAdapter = new HomeFragmentPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(homeFragmentPagerAdapter);

        tabLayout.setupWithViewPager(viewPager);


        viewPager.setCurrentItem(1);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    private List<String> pageItems = new ArrayList<>();


    class HomeFragmentPagerAdapter extends FragmentPagerAdapter {

        public HomeFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return pageItems.get(position);
        }
        @Override
        public Fragment getItem(int position) {
            RecyclerViewFragment recyclerViewFragment = new RecyclerViewFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("index", position);
            recyclerViewFragment.setArguments(bundle);
            return recyclerViewFragment;
        }


        @Override
        public int getCount() {
            return pageItems.size();
        }
    }


}
